package 设计二构造版本2;

import java.util.Scanner;

/**
 * 习题集系统类，提供程序的主入口
 */
public class ExerciseSystem {
    
    /**
     * 主方法，程序入口点
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        exercises exerciseSet = new exercises();
        
        System.out.println("========== 欢迎使用加减法习题生成系统（版本2.0）==========\n");
        
        boolean running = true;
        while (running) {
            displayMenu();
            int choice = getUserChoice(scanner);
            
            switch (choice) {
                case 1:
                    generateMixedExercises(scanner, exerciseSet);
                    break;
                case 2:
                    generateAddExercises(scanner, exerciseSet);
                    break;
                case 3:
                    generateSubtractExercises(scanner, exerciseSet);
                    break;
                case 4:
                    exerciseSet.displayExercisesWithoutAnswers();
                    break;
                case 5:
                    exerciseSet.displayExercisesWithAnswers();
                    break;
                case 6:
                    displayExercisesInColumns(scanner, exerciseSet);
                    break;
                case 7:
                    exerciseSet.statistics();
                    break;
                case 0:
                    running = false;
                    System.out.println("\n感谢使用，再见！");
                    break;
                default:
                    System.out.println("\n无效的选择，请重新输入！\n");
            }
        }
        
        scanner.close();
    }
    
    /**
     * 显示主菜单
     */
    private static void displayMenu() {
        System.out.println("请选择操作：");
        System.out.println("1. 生成混合加减法习题");
        System.out.println("2. 仅生成加法习题");
        System.out.println("3. 仅生成减法习题");
        System.out.println("4. 显示习题（不显示答案）");
        System.out.println("5. 显示习题及答案");
        System.out.println("6. 多列格式显示习题");
        System.out.println("7. 查看习题统计信息");
        System.out.println("0. 退出系统");
        System.out.print("请输入选择: ");
    }
    
    /**
     * 获取用户选择
     * @param scanner 扫描器
     * @return 用户选择的整数
     */
    private static int getUserChoice(Scanner scanner) {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1; // 无效输入
        }
    }
    
    /**
     * 生成混合加减法习题
     * @param scanner 扫描器
     * @param exerciseSet 习题集
     */
    private static void generateMixedExercises(Scanner scanner, exercises exerciseSet) {
        System.out.print("请输入要生成的习题数量: ");
        try {
            int count = Integer.parseInt(scanner.nextLine());
            if (count <= 0) {
                System.out.println("\n数量必须大于0！\n");
                return;
            }
            
            exerciseSet.generateExercises(count);
            System.out.printf("\n成功生成%d道混合加减法习题！\n\n", count);
        } catch (NumberFormatException e) {
            System.out.println("\n请输入有效的数字！\n");
        }
    }
    
    /**
     * 生成加法习题
     * @param scanner 扫描器
     * @param exerciseSet 习题集
     */
    private static void generateAddExercises(Scanner scanner, exercises exerciseSet) {
        System.out.print("请输入要生成的加法习题数量: ");
        try {
            int count = Integer.parseInt(scanner.nextLine());
            if (count <= 0) {
                System.out.println("\n数量必须大于0！\n");
                return;
            }
            
            // 询问是否清空现有习题
            System.out.print("是否清空现有习题？(y/n): ");
            String clearChoice = scanner.nextLine().toLowerCase();
            
            if (clearChoice.equals("y")) {
                exerciseSet.clear();
            }
            
            exerciseSet.generateAddExercises(count);
            System.out.printf("\n成功生成%d道加法习题！\n\n", count);
        } catch (NumberFormatException e) {
            System.out.println("\n请输入有效的数字！\n");
        }
    }
    
    /**
     * 生成减法习题
     * @param scanner 扫描器
     * @param exerciseSet 习题集
     */
    private static void generateSubtractExercises(Scanner scanner, exercises exerciseSet) {
        System.out.print("请输入要生成的减法习题数量: ");
        try {
            int count = Integer.parseInt(scanner.nextLine());
            if (count <= 0) {
                System.out.println("\n数量必须大于0！\n");
                return;
            }
            
            // 询问是否清空现有习题
            System.out.print("是否清空现有习题？(y/n): ");
            String clearChoice = scanner.nextLine().toLowerCase();
            
            if (clearChoice.equals("y")) {
                exerciseSet.clear();
            }
            
            exerciseSet.generateSubtractExercises(count);
            System.out.printf("\n成功生成%d道减法习题！\n\n", count);
        } catch (NumberFormatException e) {
            System.out.println("\n请输入有效的数字！\n");
        }
    }
    
    /**
     * 多列格式显示习题
     * @param scanner 扫描器
     * @param exerciseSet 习题集
     */
    private static void displayExercisesInColumns(Scanner scanner, exercises exerciseSet) {
        if (exerciseSet.getExerciseCount() == 0) {
            System.out.println("\n当前没有习题，请先生成习题！\n");
            return;
        }
        
        System.out.print("请输入要显示的列数 (建议: 2-4): ");
        try {
            int columns = Integer.parseInt(scanner.nextLine());
            if (columns < 1) {
                System.out.println("\n列数必须大于0！\n");
                return;
            }
            
            exerciseSet.displayExercisesInColumns(columns);
        } catch (NumberFormatException e) {
            System.out.println("\n请输入有效的数字！\n");
        }
    }
}